<template>
	<view class="userprofile">
		<u-navbar left-icon-color="#fff" :placeholder="true" :bgColor="bgColor" :autoBack="true">
			<view class="navtitle" slot="center">
				好友资料
			</view>
		</u-navbar>
		<view class="useu-info" v-if="user">
			<view class="user-left">
				<u--image :showLoading="true" radius="10" :src="user.u_img" loadingIcon="/static/chat/logo.png"
					errorIcon="/static/chat/logo.png" width="55px" height="55px"></u--image>
			</view>
			<view class="user-right">
				<view class="title">{{user.berak || user.user}}</view>
				<view class="mintitle">用户账号:{{user.user}}</view>
			</view>
		</view>
		<view class="user-name" @click="show = true">
			<view>设置备注</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="user-name">
			<view>消息免打扰</view>
			<u-switch v-model="switchvalue" @change="change"></u-switch>
		</view>
		<view class="fun-btn">
			<u-button type="error" class="btn" size="large" text="删除好友" @click="deleteuser"></u-button>
		</view>
		<u-popup :show="show" @close="close" mode="center" bgColor="transparent">
			<view class="input-box">
				<view class="title">设置备注</view>
				<u--input placeholder="请输入对方备注" customStyle="background-color: #424242; padding: 20rpx; width:450rpx;"
					prefixIconStyle="color: #fff;" prefixIcon="search" :maxlength="11" color="#fff" border="none"
					v-model="value"></u--input>
				<view class="btn">
					<u-button type="primary" color="#5FBC94" @click="gobuy" text="确定"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: 'rgba(0,0,0,0.0)',
				id: '',
				user: {},
				show: false,
				value: '',
				switchvalue:false,
				timevalue:false,
			}
		},
		onLoad(e) {
			this.id = e.id
			this.getuserinfo()
			this.getGroupNotificationStatus()
		},
		methods: {
			getGroupNotificationStatus() {
				const conversationType = this.$RongIMLib.ConversationType.PRIVATE;
				const targetId = this.id;
				this.$RongIMLib.getConversationNotificationLevel({conversationType,targetId}).then(
					res => {
						if (res.code == 0) {
							console.log(res)
							if (res.data == 5) {
								this.switchvalue = true
							} else {
								this.switchvalue = false
							}
						}
					}).catch(err=>{
						console.log(err)
						setTimeout(()=>{
							this.getGroupNotificationStatus()
						},1000)
					})
			},
			change(e) {
				console.log(e)
				if (e == true) {
					const conversationType = this.$RongIMLib.ConversationType.PRIVATE
					const targetId = this.id
					const notificationLevel = this.$RongIMLib.NotificationLevel.NOT_MESSAGE_NOTIFICATION
					this.$RongIMLib.setConversationNotificationLevel({conversationType, targetId},notificationLevel).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title:'设置成功！',
								icon:'none'
							})
						} else {
							uni.showToast({
								title:'设置失败！',
								icon:'none'
							})
						}
					})
				}else{
					const conversationType = this.$RongIMLib.ConversationType.PRIVATE
					const targetId = this.id
					const notificationLevel = this.$RongIMLib.NotificationLevel.ALL_MESSAGE
					this.$RongIMLib.setConversationNotificationLevel({conversationType, targetId},notificationLevel).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title:'设置成功！',
								icon:'none'
							})
						} else {
							uni.showToast({
								title:'设置失败！',
								icon:'none'
							})
						}
					})
				}
			},
			deleteuser(){
				let app = this
				uni.showModal({
					title: '提示',
					content: '是否删除好友！',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.showLoading({
								mask:true
							})
							app.$Request({
								method: 'POST',
								url: '/api/rong/delFriends',
								data: {
									member_id: app.id,
								}
							}).then(res => {
								uni.hideLoading()
								if (res.code == 1) {
									console.log(res)
									const conversationType = app.$RongIMLib.ConversationType.PRIVATE;
									const targetId = app.id;
									app.$RongIMLib.removeConversation({
									    conversationType,
									    targetId: targetId,
									}).then(res => {
									    // 删除指定会话成功
									    if(res.code === 0){
									        console.log(res.code)
											uni.showToast({
												title:'删除成功！',
												icon:'none'
											})
											setTimeout(()=>{
												uni.navigateBack({
													delta:2
												})
											},1000)
									    } else {
									        console.log(res.code, res.msg)
									    }
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			gobuy(){
				if(this.value == ''){
					uni.showToast({
						title:'请输入对方备注！',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					mask:true
				})
				this.$Request({
					method: 'POST',
					url: '/api/rong/setBreak',
					data: {
						member_id: this.id,
						name:this.value
					}
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.show = false
						uni.showToast({
							title:'设置成功！',
							icon:'none'
						})
						this.getuserinfo()
					}
				})
				
			},
			close() {
				this.show = false
				// console.log('close');
			},
			getuserinfo() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getUserInfo',
					data: {
						use_id: this.id,
					}
				}).then(res => {
					if (res.code == 1) {
						this.user = res.data
					}
				})

			}
		}
	}
</script>
<style>
	page {
		background-color: #17191A;
	}
</style>
<style scoped lang="scss">
	.userprofile {
		.fun-btn{
			width: 92%;
			margin: 0 auto;
			.btn{
				margin-top: 30rpx;
			}
		}
		.navtitle {
			color: #fff;
		}

		.input-box {
			background-color: #232526;
			padding: 30rpx;
			color: #fff;
			border-radius: 20rpx;

			.title {
				padding-bottom: 30rpx;
				text-align: center;
			}

			.btn {
				margin-top: 30rpx;
			}
		}

		.useu-info {
			display: flex;
			align-items: center;
			padding: 40rpx 30rpx;

			.user-left {
				margin-right: 20rpx;

				image {
					width: 110rpx;
					height: 110rpx;
				}
			}

			.user-right {
				.title {
					font-size: 32rpx;
					color: #fff;
					font-weight: 600;
				}

				.mintitle {
					margin-top: 10rpx;
					font-size: 28rpx;
					color: #c3c3c3;
				}
			}
		}

		.user-name {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20rpx 30rpx;
			color: #fff;
			background-color: #232526;
			padding: 25rpx 20rpx;
			border-radius: 30rpx;
		}
	}
</style>